package cn.wolfcode.luowowo.comment.service.impl;

import cn.wolfcode.luowowo.comment.domain.ScenicComment;
import cn.wolfcode.luowowo.comment.repository.IScenicCommentRepository;
import cn.wolfcode.luowowo.comment.service.IScenicCommentService;
import cn.wolfcode.luowowo.scenic.query.ScenicCommentQueryObject;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.util.StringUtils;

import java.util.*;

@Service
public class ScenicCommentServiceImpl implements IScenicCommentService {
    @Autowired
    private MongoTemplate mongoTemplate;
    @Autowired
    private IScenicCommentRepository repository;

    public ScenicComment save(ScenicComment scenicComment) {
        if(scenicComment.getId() == null){
            scenicComment.setCreateTime(new Date());
        }
        // 判断有没有图片
        if(StringUtils.hasLength(scenicComment.getPhotoUrl())){
            scenicComment.setHasPhoto(ScenicComment.HAS_PHOTO);
        }
        repository.save(scenicComment);
        return scenicComment;
    }

    @Override
    public ScenicComment get(String id) {
        return null;
    }

    public Page<ScenicComment> query(ScenicCommentQueryObject qo) {
        // 封装查询对象
        Query query = new Query();
        // 条件封装对象
        Criteria criteria = null;
        // 攻略文章条件
        if(qo.getScenicId() != -1){
            criteria = Criteria.where("scenicId").is(qo.getScenicId());
        }
        if(qo.getTagCondition().equals(ScenicCommentQueryObject.HAS_PHOTO)){
            criteria = Criteria.where("hasPhoto").is(1);
        } else if (qo.getTagCondition().equals(ScenicCommentQueryObject.NEGATIVE_COMMENT)){
            criteria = Criteria.where("synStarnum").gte(1).lte(2);
        } else if (qo.getTagCondition().equals(ScenicCommentQueryObject.MODERATE_COMMENT)){
            criteria = Criteria.where("synStarnum").gte(3).lte(4);
        } else if (qo.getTagCondition().equals(ScenicCommentQueryObject.POSITIVE_COMMENT)){
            criteria = Criteria.where("synStarnum").is(5);
        }

        // 关键字查询
        if(StringUtils.hasLength(qo.getKeyword())){
            criteria.and("content").regex("^.*" + qo.getKeyword() + ".*$");
        }

        // 将条件封装对象 整合到 查询对象中
        query.addCriteria(criteria);

        // 通过设置好的查询对象, 查询所有符合条件的评论总条数
        long total = mongoTemplate.count(query, ScenicComment.class);
        // 如果没有查到, 返回一个空分页对象
        if(total == 0){
            return  Page.empty();
        }

        // 分页对象
        Pageable pageable= PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize());
        // 为查询对象添加分页条件
        query.with(pageable);

        // 排序对象
        Sort sort = Sort.by(Sort.Direction.DESC, "createTime");
        // 为查询对象添加排序条件
        query.with(sort);

        // 分页&排序&高级查询
        List<ScenicComment> list = mongoTemplate.find(query, ScenicComment.class);

        // 返回PageImpl
        // 1. 分页元素集合, 分页对象, 总查询数
        return new PageImpl<ScenicComment>(list, pageable, total);
    }

    @Override
    public Map<String, Long> getCount(Long id) {

        HashMap<String, Long> map = new HashMap<>();

        long commentNum = repository.countByScenicId(id);
        long commentWithPhotoNum = repository.countByScenicIdAndHasPhoto(id, ScenicComment.HAS_PHOTO);
        long nagetiveCommentNum =  repository.countByScenicIdAndSynStarnum(id, 1) + repository.countByScenicIdAndSynStarnum(id, 2);
        long moderateCommentNum =  repository.countByScenicIdAndSynStarnum(id, 3) + repository.countByScenicIdAndSynStarnum(id, 4);
        long positiveCommentNum =  repository.countByScenicIdAndSynStarnum(id, 5);

        map.put("commentNum", commentNum);
        map.put("commentWithPhotoNum", commentWithPhotoNum);
        map.put("nagetiveCommentNum", nagetiveCommentNum);
        map.put("moderateCommentNum", moderateCommentNum);
        map.put("positiveCommentNum", positiveCommentNum);

        return map;
    }

    @Override
    public long getCountWithPhoto(long id) {
        return repository.countByScenicIdAndHasPhoto(id, ScenicComment.HAS_PHOTO);
    }
}
